/*
 * @lc app=leetcode.cn id=4 lang=golang
 *
 * [4] 寻找两个正序数组的中位数
 */

// @lc code=start
package main

func findMedianSortedArrays(l1 []int, l2 []int) (out float64) {
	n, m := len(l1), len(l2)
	ls := make([]int, n+m)
	var i, j, k int
	for ; k < n+m; k++ {
		if i < n && j < m {
			if l1[i] < l2[j] {
				ls[k] = l1[i]
				i++
			} else {
				ls[k] = l2[j]
				j++
			}
		} else if i < n {
			ls[k] = l1[i]
			i++
		} else if j < m {
			ls[k] = l2[j]
			j++
		}
	}
	if k%2 == 1 {
		return float64(ls[k/2])
	} else {
		return float64(ls[k/2-1]+ls[k/2]) / 2
	}
}

// @lc code=end
